﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using webapi;
using webapi.Models;
using System.Data;
using webapi.Entity;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace webapi.Controllers
{
    public class CourseController : ApiController
    {
      //更新课程
      public IHttpActionResult updateCourse([FromBody] JObject obj)
      {
        Course course = JsonConvert.DeserializeObject<Course>(obj["course"].ToString());
        int i = CourseService.updateCourse(course);
        return Json<dynamic>(new { code=0, i, msg=true });
      }

      //添加课程
      [HttpPost]
      public IHttpActionResult insertCourse([FromBody] JObject obj)
      {
        Course course = JsonConvert.DeserializeObject<Course>(obj["course"].ToString());
        int i = CourseService.insertCourse(course);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code=0, msg });
      }

      //获取全部课程
      [HttpGet]
      public IHttpActionResult getCourse(int curr, int limit)
      {
        List<Course> courses = new List<Course>();
        DataRowCollection dr = CourseService.getAllCourse();
        int j = curr - 1;
        int count = dr.Count;
        int len = count;
        if (count > limit)
        {
          int z = count - (j * limit);
          len = z >= limit ? limit : z ;
        }
        for (int i = j * limit; i < len + j * limit; i++)
        {
          courses.Add(new Course
          {
            course_id = dr[i][1].ToString().Trim(),
            name = dr[i][2].ToString().Trim(),
            point = (double)dr[i][3],
            is_must = (int)dr[i][4]
          });
        }
        return Json<dynamic>(new { code = 0, count, data = courses });
      }

      //获取开设的课程
      public IHttpActionResult getSetUpCourse()
      {
        List<SetUpCourse> set_up_courses = new List<SetUpCourse>();
        DataRowCollection dr = CourseService.getAllSetUpCourse();
        int len = dr.Count;
        for (int i = 0; i < len; i++)
        {
          set_up_courses.Add(new SetUpCourse
          {
            course_id = dr[i][1].ToString().Trim(),
            time = (int)dr[i][2],
            class_course_id = dr[i][3].ToString().Trim(),
            start = (int)dr[i][4],
            last = (int)dr[i][5],
            day = (int)dr[i][6],
            //count = (int)dr[i][7]
          });
        }
        return Json<dynamic>(new { set_up_courses });
      }

      //删除课程
      [HttpPost]
      public IHttpActionResult delCourse([FromBody] JObject obj)
      {
        string arr = obj["ids"].ToString();
        JArray ids = (JArray)JsonConvert.DeserializeObject(arr);
        int a = 0;
        foreach (string item in ids)
        {
          a += CourseService.deleteCourse(item);
        }
        return Json<dynamic>(new { msg=true, code=0, a});
      }

      //按课程名搜索课程
      [HttpPost]
      public IHttpActionResult searchByName([FromBody] JObject obj)
      {
        string name = obj["name"].ToString();
        List<Course> courses = new List<Course>();
        DataRowCollection dr = CourseService.getCoursesByName(name);
        int len = dr.Count;
        for (int i = 0; i < len; i++)
        {
          courses.Add(new Course
          {
            course_id = dr[i][1].ToString().Trim(),
            name = dr[i][2].ToString().Trim(),
            point = (double)dr[i][3],
            is_must = (int)dr[i][4]
          });
        }
        return Json<dynamic>(new { code = 0, count = len, data = courses });
      }
    }
}
